package com.abhipedro.wordsearch.view.runnables;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.abhipedro.wordsearch.model.Preferences;
import com.abhipedro.wordsearch.view.R;

public class OnlineHighScore extends Activity {
 
    private Preferences prefs;
    public static final String PREFS_NAME = "prefs";
    private static final int MENU_QUIT = 1;
    private WebView webview;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.activity_scores);
//      webview = new WebView(this);
        webview = (WebView) this.findViewById(R.id.webView);
        prefs = new Preferences(this);
     
        webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        webview.getSettings().setJavaScriptEnabled(true);
//      setContentView(webview);
         
 
        final Activity activity = this;
        webview.setWebChromeClient(new WebChromeClient() {
            Boolean entrou1Vez=false; // enter only once
            ProgressDialog myProgressDialog = null;
            public void onProgressChanged(WebView view, int progress) {
                //activity.setProgress(progress * 1000);
                if (!entrou1Vez)
                {
                    myProgressDialog = ProgressDialog.show(OnlineHighScore.this,     
                        "Please wait...", "Loading online content..", true);
                    entrou1Vez=true;
                }
                 
                // This causes crash
                if ((progress >30) && (myProgressDialog != null))
                    try {
                        myProgressDialog.dismiss();
                    } catch (Exception e) {
 
                    }
            }
        });
        webview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                Toast.makeText( activity,"Error communicating with server! Make sure you're connected to the internet."
                                + description, Toast.LENGTH_LONG).show();
            }
        });
 
        webview.loadUrl(prefs.getOnlineURL());
 
    }
     
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
     
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_QUIT, 0, "Game Menu");
        return true;
    }
 
    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_QUIT:
            finish();
            return true;
        }
        return false;
    }
 
}